import {createWebHashHistory, RouterScrollBehavior} from "vue-router";
import {RouterExtraOptions, RouterOptions} from "./app-router-type.js";

export const defaultRouterOptions: Partial<RouterOptions> = {
  history: createWebHashHistory(),
  scrollBehavior(to, from, savedPosition): ReturnType<RouterScrollBehavior> {
    const matchRoutes = to.matched.filter((item) => {
      return item.path === to.path;
    });

    if (matchRoutes.length > 0) {
      const matchRoute = matchRoutes[0];
      if (typeof matchRoute.meta.scrollBehavior === "function") {
        return to.meta.scrollBehavior(to, from, savedPosition);
      }
    }

    if (savedPosition) {
      return savedPosition;
    }
    const result: any = {
      left: 0,
      top: 0,
      behavior: "smooth",
    };

    if (to.hash) {
      result.el = to.hash;
    }
    return result;
  },
};

export const defaultRouterExtraOptions: Required<RouterExtraOptions> = {
  showLoading: false,
  showLoadingFunction() {},
  hideLoadingFunction() {},
  afterShowLoading() {},
  allowDefault: false,
  afterNavigation() {},
};
